package com.okaysoft.ofb.account

import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON

class RoleController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def springSecurityService
    def sysService
    
    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        def rms = Requestmap.findAllByMenuEnabled(Boolean.TRUE)
        [requestmaps: rms]
    }

    def listJson() {
        params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        params.page = params.page ? params.int('page') : 0
        params.offset = (params.page - 1) * params.max
        def roles = Role.list(params)
        roles = roles.collect {role->
            def rms
            def rmid = []
            role.requestmaps.each {rm->
                if (rms) {
                  rms+=","+rm.name
                }else{
                    rms = rm.name
                }
                rmid << "${rm.id}"
            }
            [id:role.id,name:role.name,authority:role.authority,requestmap:rms,requestmapid:rmid]
        }
        def count = Role.count()
        render([total: count, rows: roles] as JSON)
    }

    def save() {
        def ids = params.requestmapId
        def roleInstance = Role.get(params.id)
        if(!roleInstance) {
            roleInstance = new Role()
        }
        try {
            sysService.updateRole(roleInstance,params)
        } catch (RuntimeException e) {
            log.error(e.getMessage(),e)
            def failure = [status: 0, msg: message(code: 'default.save.failure.message', default: 'Save failure')] as JSON
            render(failure.toString())
            return
        }
        def succ = [status: 1, msg: message(code: 'default.save.success.message', default: 'Save success')] as JSON
        render(succ.toString())
    }

    def show() {
        def roleInstance = Role.get(params.id)
        if (!roleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), params.id])
            redirect(action: "list")
            return
        }

        [roleInstance: roleInstance]
    }

    def delete() {
        def roleInstance = Role.get(params.id)
        if (!roleInstance) {
            render([status: 0, msg:  message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), params.id])] as JSON)
            return
        }

        try {
            //delete user role

            springSecurityService.deleteRole(roleInstance)
            springSecurityService.clearCachedRequestmaps()

            render([status: 1, msg:  message(code: 'default.deleted.message', args: [message(code: 'role.label', default: 'Role'), params.id])] as JSON)

        }
        catch (DataIntegrityViolationException e) {
            render([status: 0, msg:  message(code: 'default.not.deleted.message', args: [message(code: 'role.label', default: 'Role'), params.id])] as JSON)

        }
    }
}
